package com.dwxt.cdms.admin.controller;


import com.dwxt.cdms.admin.service.MenuServiceAdmin;
import com.dwxt.cdms.entity.Menu;
import com.dwxt.cdms.entity.vo.MenuVO;
import com.dwxt.common.annotation.ValidateFiled;
import com.dwxt.common.annotation.ValidateGroup;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cdms/menu")
public class MenuControllerAdmin {

    @Autowired
    private MenuServiceAdmin menuServiceAdmin;
    @PostMapping("/createOrUpdateMenu")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, filedName = "name", notNull = true),
            @ValidateFiled(index = 0, filedName = "parentId", notNull = true),
    })
    public BaseResult createOrUpdateMenu(Menu menu) {
        menuServiceAdmin.createOrUpdateMenu(menu);
        return BaseResult.ok();
    }

    @DeleteMapping("/delete")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, notNull = true)
    })
    public BaseResult delete(Integer[] id) {
        int i = menuServiceAdmin.batchDeleteById(id);
        return i == 0 ? BaseResult.build(StatusCode.MENU_NOT_EXIST) : BaseResult.ok();
    }

    @GetMapping("/pageList")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, notNull = true),
            @ValidateFiled(index = 1, notNull = true),
    })
    public BaseResult pageList(Integer pageSize, Integer pageNum,Integer parentId) {
        PageHelper.startPage(pageNum, pageSize);
        List<Menu> menus = menuServiceAdmin.selectAll(parentId);
        PageInfo<Menu> pageInfo = new PageInfo<>(menus);
        return BaseResult.ok(pageInfo);
    }

    /**
     * 获取所有顶级菜单
     */
    @GetMapping("/allTopMenu")
    public BaseResult allTopMenu() {
        //RedisUtil.set("123","123");
        List<MenuVO> menus = menuServiceAdmin.selectAllTopMenu();
        return BaseResult.ok(menus);
    }


    @RequestMapping("/menuIdList")
    public BaseResult menuIdList(Integer id){
        return menuServiceAdmin.menuIdList(id);
    }

}
